package info.techgeek.labs.date;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class SimpleDateFormatTest {
	
	static String SDF_DATA_TYPE_DATE_FORMAT_DT = "yyyy-MM-dd";
	static String SDF_DATA_TYPE_DATE_FORMAT_DT_TI = "yyyy-MM-dd HH:mm:ss";
	
	private static SimpleDateFormat simpleDateFormat_dt = new SimpleDateFormat(SDF_DATA_TYPE_DATE_FORMAT_DT);
	private static SimpleDateFormat simpleDateFormat_dt_ti = new SimpleDateFormat(SDF_DATA_TYPE_DATE_FORMAT_DT_TI);
	
	public static void main(String[] args) {
		simpleDateFormat_dt.setLenient(false);
		simpleDateFormat_dt_ti.setLenient(false);
		String dateInStrin = "2009-08-28 12:12:12";
//		String dateInStrin = "28-08-2009 12:12:12";
//		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
		
//		simpleDateFormat.setLenient(true);
//		try {
//			simpleDateFormat.parse(simpleDateFormat.format(dateInStrin));
//			System.out.println("Pass-->>\n\t"+ simpleDateFormat.parse(dateInStrin));
//		} catch (ParseException e) {
//			System.err.println("Fail");
//		}
		
		
		System.out.println("Date "+getDateValue(dateInStrin));
		System.out.println("TimeStamp "+getTimestampValue(dateInStrin));
	}

	
	private static Date getDateValue(java.util.Date utilDate) {
		Date date = null;
		if (utilDate != null) {
			date = new java.sql.Date(utilDate.getTime());
		}
		return date;
	}

	private static Date getDateValue(String string)  {
		Date date = null;
		try {
			if(string != null){
				String stringDate = string.toString().trim(); 
				if (!("".equals(stringDate))) {
//					java.util.Date utilDate = simpleDateFormat_dt.parse(stringDate);
//					date = new java.sql.Date(utilDate.getTime());
					if(stringDate.length() > 10 ){
						java.util.Date utilDate = simpleDateFormat_dt_ti.parse(stringDate.trim());
						date = new java.sql.Date(utilDate.getTime());
					}else{
						java.util.Date utilDate = simpleDateFormat_dt.parse(stringDate.trim());
						date = new java.sql.Date(utilDate.getTime());
					}
				}
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	private static Timestamp getTimestampValue(java.util.Date doubleString)  {
		Timestamp timestamp = null;
		if (doubleString != null) {
			timestamp = new Timestamp(doubleString.getTime());
//			timestamp = getTimestampValue(simpleDateFormat_dt_ti.format(doubleString));
		}
		return timestamp;
	}

	private static Timestamp getTimestampValue(String string)  {
		Timestamp timestamp = null;
		try {
			if(string != null){
				String stringDate = string.toString().trim(); 
				if(!("".equals(stringDate) )){
					if (!("".equals(stringDate))) {
						java.util.Date utilDate;
						utilDate = simpleDateFormat_dt_ti.parse(stringDate);
						timestamp = new Timestamp(utilDate.getTime());
					}
				}
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return timestamp;
	}
	
}
